﻿#ifndef QTSERIAL_H
#define QTSERIAL_H

#include <QMainWindow>
#include <QSerialPort>
#include <QSerialPortInfo>


namespace Ui {
class qtSerial;
}

typedef struct SerialSpeed
{
    double send_spped;
    double recevie_speed;
}SerialSpeed;

typedef struct SerialData
{
    qint64 send_data;
    qint64 recevie_data;
}SerialData;

class qtSerial : public QMainWindow
{
    Q_OBJECT

public:
    explicit qtSerial(QWidget *parent = nullptr);
    ~qtSerial();
    void initSreial();
    void sendHex(QString a, QByteArray sendBytes);
    SerialSpeed statiticsSpeed(int speed_time_ms, SerialData &data);

private slots:
    void receDataSlot();
    void openSerialPortSlot();
    void refreshSerialPortSlot();
    void clearRecvSlot();
    void stopDisplaySlot();
    void sendDataSlot();
    void autoSend(int state);
    void printSpeed();

private:
    Ui::qtSerial *ui;
    QSerialPort *serial;
    QTime *time;
    QTime *speed_old;   // 用来辅助速率统计
    QTime *speed_new;   // 用来辅助速率统计
    QTimer *speed_timer;
    QTimer *auto_send_timer;
    QByteArray SendBytes;
    qint64 send_total;
    qint64 receive_total;
    SerialSpeed *serial_speed;
    SerialData  *serial_data;
    bool stop_display_flag;
};

#endif // QTSERIAL_H
